<template>
  <div>
    <el-form :model="currentRecord"
             ref="editForm"
             label-width="80px"
             :rules="formRules">
            <el-form-item label="设备编号"
                    prop="equipmentId">
        <el-col :span="22">
          <el-input v-model="currentRecord.equipmentId"
                    :disabled="this.mode=='details'" placeholder="请输入设备编号"></el-input>
        </el-col>
      </el-form-item>

      <el-form-item label="部门ID"
                    prop="departmentId">
        <el-col :span="22">
          <el-input v-model="currentRecord.departmentId"
                    :disabled="this.mode=='details'" placeholder="请输入部门ID"></el-input>
        </el-col>
      </el-form-item>

      <el-form-item label="检查人ID"
                    prop="checkPersonId">
        <el-col :span="22">
          <el-input v-model="currentRecord.checkPersonId"
                    :disabled="this.mode=='details'" placeholder="请输入检查人ID"></el-input>
        </el-col>
      </el-form-item>

      <el-form-item label="检查人姓名"
                    prop="checkPersonName">
        <el-col :span="22">
          <el-input v-model="currentRecord.checkPersonName"
                    :disabled="this.mode=='details'" placeholder="请输入检查人姓名"></el-input>
        </el-col>
      </el-form-item>

    <el-form-item label="检查时间" prop="checkTime">
      <el-col :span="22">
        <el-date-picker
            v-model="currentRecord.checkTime"
            :disabled="this.mode=='details'"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间">
        </el-date-picker>
      </el-col>
      </el-form-item>

      <el-form-item label="检查结果(1:正常,0:异常)"
                    prop="checkResult">
        <el-col :span="22">
          <el-input v-model="currentRecord.checkResult"
                    :disabled="this.mode=='details'" placeholder="请输入检查结果(1:正常,0:异常)"></el-input>
        </el-col>
      </el-form-item>

      <el-form-item label="检查备注"
                    prop="checkNotes">
        <el-col :span="22">
          <el-input v-model="currentRecord.checkNotes"
                    :disabled="this.mode=='details'" placeholder="请输入检查备注"></el-input>
        </el-col>
      </el-form-item>

  <el-form-item align="center">
    <el-col :span="24">
      <el-button type="primary"
                 :loading="editLoading"
                 @click.native.prevent="handleSubmit">
        <i class="fa fa-save"></i> 保存
      </el-button>
    </el-col>
  </el-form-item>
  </el-form>
  </div>
</template>

<script>
import {Message} from 'element-ui'
import EquipmentCheckApi from '../api/equipmentCheck'

export default {
  name: 'EquipmentCheckEdit',
  props: {
    mode: {
      type: String
    },
    record: {
      type: Object
    }
  },
  data() {
    return {
      currentRecord: this.record,
      formRules: {},
      editLoading: false
    }
  },
  watch: {
    record: function (oldValue, newValue) {
      this.currentRecord = this.record
      // this.getDetail()
    }
  },
  methods: {

    handleSubmit() {
      this.$refs['editForm'].validate(valid => {
        if (valid) {
          let newRecord = Object.assign({}, this.currentRecord)
          if (this.mode === 'add') {
            this.editLoading = true
            EquipmentCheckApi.add(newRecord).then(rspData => {
              if (rspData.data.code === 200) {
                Message.success('新增成功')
              } else {
                Message.error(rspData.data.message)
              }
              this.editLoading = false
              this.$emit('after-edit')
            })
          } else if (this.mode === 'details') {
            this.$emit('after-edit')
          } else {
            this.editLoading = true
            EquipmentCheckApi.update(newRecord).then(rspData => {
              if (rspData.data.code === 200) {
                Message.success('修改成功')
              } else {
                Message.error(rspData.data.message)
              }
              this.editLoading = false
              this.$emit('after-edit')
            })
          }
        } else {
          return false
        }
      })
    },
    resetFields() {
      this.$refs['editForm'].resetFields()
    }
  }
}
</script>
